<template>
  <div class="app-container">
    <el-card v-if="elderList"  style="height: 690px">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd()"
        >新增</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch"
               label-width="68px" style="display: flex; justify-content: flex-end;">
        <el-form-item label="姓名" prop="elderName">
          <el-input
            v-model="queryParams.elderName"
            placeholder=""
            clearable
            @keyup.enter.native="handleQuery"
            style="width: 100px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <div style="margin-left:1%;margin-right:1%">
        <el-row>
          <el-col :span="4" v-for="(item) in elderInfoList" :key="item.elderId" :offset="0" >
            <div class="card-container">
                <el-card :body-style="{ padding: '0px'}" shadow="hover">
                  <img :src="item.elderImg" class="image" style="height: 90px;width: 150px;">
                  <div class="e-info">
                    <span>姓名:{{item.elderName}}</span><br>
                    <span>性别:{{item.elderGender}}</span><br>
                    <span>公寓:{{item.apartmentName}}</span><br>
                    <span>房间编号:{{item.roomCode}}:{{item.bedCode}}</span><br>
                    <div class="bottom clearfix">
                      <time class="time"><strong>入住状态:</strong>{{item.liveStatus}}</time>
                      <el-button type="danger" icon="el-icon-delete" plain class="button" @click="handleDeleteElder(item.elderId)"></el-button><span> </span>
                      <el-button type="success" icon="el-icon-edit" plain class="button" @click="handleUpdate(item)"></el-button><span> </span>
                    </div>
                  </div>
                </el-card>
            </div>
          </el-col>
        </el-row>
      </div>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :page-sizes="[12, 24, 36, 48, 60, 72]"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <elder-info v-if="detailInfo"  :formData="formData" @return-value="handleValue"></elder-info>
    <add-elder v-if="addElderInfo"  :formData="formData" @return-value="handleValue"></add-elder>

  </div>
</template>

<script>
import { listElderInfo, getElderInfo, delElderInfo, addElderInfo, updateElderInfo } from "@/api/business/elderInfo";
import {parseTime} from "@/utils/ruoyi";
import elderInfo from "@/views/business/elderInfo/info"
import addElder from "@/views/business/elderInfo/addElder"

export default {
  name: "ElderInfo",
  components: {
    elderInfo,
    addElder,
  },
  data() {
    return {
      formData:{},
      elderList:true,
      detailInfo:false,
      addElderInfo:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 老人信息表格数据
      elderInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        apartmentId: null,
        elderImg: null,
        elderName: null,
        elderAge: null,
        elderGender: null,
        birthDate: null,
        checkInTime: null,
        checkOutTime: null,
        roomId: null,
        bedCode: null,
        deposit: null,
        liveStatus: null,
        identityCardNumber: null,
        contactPhone: null,
        homeAddress: null,
        healthStatus: null,
        hasSurgeryHistory: null,
        hasChronicDisease: null,
        hasMentalIllness: null,
        hasInfectiousDisease: null,
        checkUserId: null,
        isUnusual: null,
        url: null,
        isDelete: null,
        notes: null,
        checkUserName: null,
        createUserId: null,
        updateUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询老人信息列表 */
    getList() {
      this.loading = true;
      listElderInfo(this.queryParams).then(response => {
        this.elderInfoList = response.rows;
        this.elderInfoList.forEach(item =>{
          if(item.checkInTime){
            item.checkInTime = parseTime(item.checkInTime);
          }
          if(item.checkOutTime){
            item.checkOutTime = parseTime(item.checkOutTime);
          }
          if(item.birthDate){
            item.birthDate = parseTime(item.birthDate);
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        elderId: null,
        apartmentId: null,
        apartmentName: null,
        elderImg: null,
        elderName: null,
        elderAge: null,
        elderGender: null,
        birthDate: null,
        checkInTime: null,
        checkOutTime: null,
        roomId: null,
        bedCode: null,
        deposit: null,
        liveStatus: null,
        identityCardNumber: null,
        contactPhone: null,
        homeAddress: null,
        healthStatus: null,
        hasSurgeryHistory: null,
        hasChronicDisease: null,
        hasMentalIllness: null,
        hasInfectiousDisease: null,
        checkUserId: null,
        isUnusual: null,
        url: null,
        isDelete: null,
        notes: null,
        checkUserName: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.$router.push({ name: 'elderInfos',query: {}});
      this.formData = {elderImg:'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'};
      this.elderList = false;
      this.addElderInfo = true;
    },
    /** 修改按钮操作 */
    handleUpdate(item) {
      const elderId = item.elderId;
      let elder = {};
      getElderInfo(elderId).then(response => {
        this.formData = response.data;
        this.elderList = false;
        this.detailInfo = true;
      })
    },
    handleValue(){
      this.getList();
      this.elderList = true;
      this.detailInfo = false;
      this.addElderInfo = false;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateElderInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addElderInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除老人信息编号为"' + ids + '"的数据项？').then(function() {
        return delElderInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleDeleteElder(elderId){
      const ids = elderId;
        this.$modal.confirm('是否确认删除该老人信息').then(function() {
        return delElderInfo(ids);
      }).then(()=>{
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
<style scoped>
  .time {
    font-size: 13px;
    color: #999;
  }
  .bottom {
    margin-top: 13px;
    line-height: 12px;
  }
  .button {
    padding: 0;
    float: right;
  }
  .image {
    width: 100%;
    display: block;
  }
  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both  }
  .e-info{
    font-size: 13px;
  }
  .card-container{
    margin-top:15px;
    width: 150px;
    height: 250px;
  }
</style>
